/**
 * Displays the clue overlay
 * Will set the "ignoreMouseBecauseOfClueOverlay" variable
 */
class tClueOverlay
{
  tShape cornerShape;
  tShape fullShape;
  // <0.05 is in the corner, >0.05 is the full screen
  float state;
  tClue clues[];
  
  tClueOverlay()
  {
    int diff = min(screenSize[0]/12, screenSize[1]/6);
    int[][] cornerCoords = {{0,0}, {diff,0}, {0,diff}};
    cornerShape = new tShape(cornerCoords, color(180,180,220,125));
    int[][] fullCoords = {{diff,0}, {screenSize[0]-diff,0}, {screenSize[0],diff}, {screenSize[0],screenSize[1]-diff},
                          {screenSize[0]-diff,screenSize[1]}, {diff,screenSize[1]}, {0,screenSize[1]-diff}, {0,diff}};
    fullShape = new tShape(fullCoords, color(180,180,220,125));
    state = 0;
  }
  
  /**
   * generates a new clue based on elementIDs
   * @param elementIDs should be a colon-seperated-values list of element ids
   *  eg 0:1:2:etc
   * @param addClue can be "assign" or "append"
   */
  void generateNewClues(String elementIDs, String addClue)
  {
    // get a new clue
    if (addClue == "assign")
    {
      tClue newClues[] = {new tClue()};
      clues = newClues;
    }
    else if (addClue == "append")
    {
      tClue newClues[] = new tClue[clues.length+1];
      for (int i = 0; i < clues.length; i++)
      {
        newClues[i] = clues[i];
      }
      newClues[newClues.length-1] = new tClue();
      clues = newClues;
    }
    else
    {
      return;
    }
    
    // add the lines to the clue
    String sentences[] = sentenceGen(adjPull(elementIDs), elementIDs);
    for (int i = 0; i < sentences.length; i++)
    {
      clues[clues.length-1].addLine(sentences[i]);
    }
  }
  
  // all of this updates the position of the fullShape
  // and the alphas of the cornerShape and fullShape
  void update(int[] mousePos)
  {
    if (cornerShape.getPointCollision(mousePos) && state < 0.05 && frameCount > 10)
    {
      clueIsActive = true;
      generateNewClues("0:1:2:3:4:5:6:7:8:9", "assign");
    }
    
    if (state > 0.95 && mouseButton == LEFT && isMouseDown)
    {
      clueIsActive = false;
      isMouseDown = false;
    }
    
    if (clueIsActive)
    {
      state += 0.05;
      state = min(state, 1.0);
    }
    else
    {
      state -= 0.05;
      state = max(state, 0);
    }
     
    if (state > 0.05 && state < 0.95)
    {
      cornerShape.setAlpha((int)(255.0 * (1.0 - state)));
      fullShape.setAlpha((int)(255.0 * state));
      int newPosition[] = { -(int)((1-state)*screenSize[0]), -(int)((1-state)*screenSize[1]) };
      fullShape.setPosition(newPosition);
    }
    else
    {
      if (state <= 0.05)
      {
        int newPosition[] = { -screenSize[0], -screenSize[1] };
        fullShape.setPosition(newPosition);
      }
      else
      {
        int newPosition[] = { 0, 0 };
        fullShape.setPosition(newPosition);
      }
    }
  }
  
  void doDraw()
  {
    cornerShape.doDraw();
    fullShape.doDraw();
    if (state > 0.95)
    {
      fill(255,255,255);
      this.writeLargeText("Clues:", screenSize[0]/2, 100);
      int numLines = 2;
      boolean isFirstClue = true;
      for (int cluei = 0; cluei < clues.length; cluei++)
      {
        clues[cluei].reset();
        if (isFirstClue)
        {
          isFirstClue = false;
        }
        else
        {
          numLines++;
        }
        while (clues[cluei].hasLine())
        {
          String ln = clues[cluei].getLine();
          this.writeLargeText(ln, screenSize[0]/2, 100 + 32*numLines);
          numLines++;
        }
      }
    }
  }
  
  void writeLargeText(String toWrite, int x, int y)
  {
    PFont font = menuFont;
    textFont(font);
    text(toWrite, x - textWidth(toWrite)/2, y);
  }
}

/**
 * Contains the text for the haikus
 */
class tClue
{
  String myLines[];
  int currentLine;
  
  void addLine(String newLine)
  {
    if (myLines != null && myLines.length > 0)
    {
      String oldLines[] = myLines;
      myLines = new String[myLines.length+1];
      for (int i = 0; i < oldLines.length; i++)
      {
        myLines[i+1] = oldLines[i];
      }
    }
    else
    {
      myLines = new String[1];
    }
    myLines[0] = newLine;
    currentLine = 0;
  }
  
  /**
   * The next time getNextLine is called, 
   * the first line will be returned
   * @see getNextLine()
   */
  void reset()
  {
    currentLine = 0;
  }
  
  /**
   * Returns the next line in the clue
   * @return tne next line in the clue, or null if there are no more lines
   * @see reset()
   */
  String getLine()
  {
    if (currentLine >= myLines.length)
    {
      return null;
    }
    
    currentLine++;
    currentLine = max(currentLine, 1);
    return myLines[currentLine-1];
  }
  
  boolean hasLine()
  {
    if (currentLine >= myLines.length)
    {
      return false;
    }
    
    return true;
  }
}
